package com.chinanet.netelementmodel;

import com.chinanet.entry.netelement.Attr;
import com.chinanet.entry.netelement.AttrType;
import com.chinanet.entry.netelement.WorkInterFace;
import com.chinanet.utils.telnet.HCNetworkTelnet;
import org.springframework.beans.factory.annotation.Value;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HCNetworkHLR_Imp implements WorkInterFace {
    @Value("${spring.secondary.datasource.jdbc-url}")
    private String url;
    @Value("${spring.secondary.datasource.username}")
    private String user;
    @Value("${spring.secondary.datasource.password}")
    private String password;

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "华为c网hlr接口";
	}

	@Override
	public String getDesciption() {
		// TODO Auto-generated method stub
		return "";
	}

	@Override
	public int getVersion() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Attr[] getInAttrs() {
		Attr[] in =new Attr[1];
		in[0] = new Attr("phonenum", "查询的手机号码，不需要加 86", AttrType.TYPE_String, true);
		return in;
	}

	@Override
	public Attr[] getOutAttrs() {
		Attr[] out = new Attr[10];
		out[0] = new Attr("flag", "操作是否成功，0：成功，1：失败", AttrType.TYPE_Int, true);
		out[1]=new Attr("ExecutionTime","执行时间",AttrType.TYPE_String,true);
		out[2]=new Attr("IMSI","IMSI",AttrType.TYPE_String,true);
		out[3]=new Attr("MDN","MDN",AttrType.TYPE_String,true);
		out[4]=new Attr("MsStatus","用户状态",AttrType.TYPE_String,true);
		out[5]=new Attr("MSCID","用户位置",AttrType.TYPE_String,true);
		out[6]=new Attr("RegTime","最后活跃时间",AttrType.TYPE_String,true);
		out[7]=new Attr("info","报错信息",AttrType.TYPE_String,true);
		out[8]=new Attr("log","日志",AttrType.TYPE_String,true);
		out[9]=new Attr("MSCIDInfo","用户位置解析地址",AttrType.TYPE_String,true);
		return out;
	}

	@Override
	public Map<String, Object> init(Map<String, Object> data) {
		Map<String,Object> outmap=new HashMap<String, Object>();
		HCNetworkTelnet twork=new HCNetworkTelnet();
//		outmap.put("flag", "0");
//		outmap.put("MSCID", "372E4C");
		outmap=twork.init("86"+String.valueOf(data.get("phonenum")));
		if(String.valueOf(outmap.get("flag")).equals("0")){
			String mscid=String.valueOf(outmap.get("MSCID"));
			if(mscid!=null && !mscid.isEmpty()){
				List<String> stringList=getHCNetworkInfo(mscid);
				for(String r:stringList){
					System.out.println(r);
				}
				if(stringList!=null && !stringList.isEmpty()) {
					outmap.put("MSCIDInfo", stringList.get(0));
				}else{
					outmap.put("MSCIDInfo", "");
				}
			}else{
				outmap.put("MSCIDInfo", "");
			}
		}else{
			outmap.put("MSCIDInfo", "");
		}



		return outmap;
	}
	public List<String> getHCNetworkInfo(String mscid){
		String sql="SELECT provinces||' - '||localnetwork from hcnetwork where mscid = ?";
		String driverName = "oracle.jdbc.driver.OracleDriver";
		try {
			// 加载驱动
			Class.forName(driverName);
			// 设置 配置数据
			// 1.url(数据看服务器的ip地址 数据库服务端口号 数据库实例)
			// 2.user
			// 3.password
			Connection conn = DriverManager.getConnection(url, user, password);
			// 开始连接数据库
			System.out.println("数据库连接成功..");
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, mscid);
			ResultSet rSet = pst.executeQuery();
			List<String> list=new ArrayList<>();
			while (rSet.next()) {
				list.add(rSet.getString(1));
			}
			return list;
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
            return null;
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
            return null;
		}
	}

}
